package com.changfeng.controller;

import com.changfeng.auth.dto.UserAuth;
import com.changfeng.bean.dto.bo.CartBO;
import com.changfeng.bean.dto.vo.CartListVO;
import com.changfeng.bean.dto.vo.CartToCommodityVO;
import com.changfeng.common.CurrentUser;
import com.changfeng.exception.ChangFengException;
import com.changfeng.exception.ChangFengExceptionEnum;
import com.changfeng.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/cart")
public class CartController {

    @Autowired
    private CartService cartService;

    @GetMapping("/addCart")
    public void addCart(Integer commodityId) {
        if (commodityId == null) {
            throw new ChangFengException(ChangFengExceptionEnum.PARAM_EXCEPTION);
        }
        UserAuth currentUser = CurrentUser.getCurrentUser();
        if (currentUser == null) {
            throw new ChangFengException(ChangFengExceptionEnum.NOT_USER);
        }
        CartBO cartBO = new CartBO();
        cartBO.setCommodityId(commodityId);
        cartBO.setUserId(currentUser.getUserId());
        cartService.addCart(cartBO);

    }

    @GetMapping("/findAll")
    public List<CartListVO> findAllByUser() {
        UserAuth currentUser = CurrentUser.getCurrentUser();
        if (currentUser == null) {
            throw new ChangFengException(ChangFengExceptionEnum.NOT_USER);
        }
       return cartService.findCartByUser(currentUser.getUserId());
    }

    @GetMapping("/delete")
    public void deleteCart(Integer cartId) {
        cartService.deleteCart(cartId);
    }

    @PostMapping("/toOrder")
    public List<CartToCommodityVO> toOrder(@RequestBody List<Integer> ids) {
        return cartService.getCommodityId(ids);
    }
}
